using System;
using System.Collections.Generic;
using System.Text;
using Microsoft.SharePoint;
using Microsoft.SharePoint.Administration;
using System.IO;

namespace SiteSecurityManagement.FeatureReceiver
{
    public class SiteSecurtyManagment:SPFeatureReceiver
    {
        #region Members
        const string JOB_TITLE = "Deploy Resources";
        const string JOB_NAME = "jobDeployResources";

        #endregion

        #region Constructor/Destructor
        #endregion

        #region Properties
        #endregion

        #region Methods
        public override void FeatureActivated(SPFeatureReceiverProperties properties)
        {
            SPWeb web = (SPWeb)properties.Feature.Parent;
            
            //Check for existing instance of the job, if exists delete it
            foreach (SPJobDefinition job in web.Site.WebApplication.JobDefinitions)
            {
                if (job.Name == JOB_NAME && job.WebApplication.Name == web.Site.WebApplication.Name)
                {
                    job.Delete();
                }
            }

            //Create new job
            DeployResources deployJob = new DeployResources(JOB_NAME, web.Site.WebApplication, properties.Definition.DisplayName);
            deployJob.Title = JOB_TITLE;

            //Set the job up to run once
            deployJob.Schedule = new SPOneTimeSchedule(DateTime.Now);
            deployJob.Update();
        }

        public override void FeatureDeactivating(SPFeatureReceiverProperties properties)
        {
            SPWeb web = (SPWeb)properties.Feature.Parent;

            //delete the job
            foreach (SPJobDefinition job in web.Site.WebApplication.JobDefinitions)
            {
                if (job.Name == JOB_NAME &&
                    job.WebApplication.Name == web.Site.WebApplication.Name)
                {
                    job.Delete();
                }
            }

            SPSite siteColl = new SPSite(web.Url);
            SPWebApplication webApp = siteColl.WebApplication;

            foreach (SPUrlZone zone in webApp.IisSettings.Keys)
            {
                //the settings of the iis application to update
                SPIisSettings oSettings = webApp.IisSettings[zone];

                //determine the dest path
                string sourcePath = Path.Combine(oSettings.Path.ToString(), "App_GlobalResources");
                string[] filePaths = Directory.GetFiles(sourcePath, "*.resx");

                //Copy Files
                foreach (string filePath in filePaths)
                {
                    if (filePath.ToLower().Contains("ssmu.resources"))
                    {
                        File.Delete(filePath);
                    }
                }
            }
        }

        public override void FeatureInstalled(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }

        public override void FeatureUninstalling(SPFeatureReceiverProperties properties)
        {
            //throw new Exception("The method or operation is not implemented.");
        }
        #endregion
    }
}
